package com.shipwe.loans.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.os.CountDownTimer
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.shipwe.loans.databinding.DialogApplyAmountGuideBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.AppDensityUtil
import com.shipwe.loans.utils.ShipweTitleBarUtil
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.thread.TimingThread

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/25 15:41
 * @Annotation：指引dialog
 */
class ApplyAmountGuideDialog: ShipweDialog {
    private lateinit var binding: DialogApplyAmountGuideBinding
    private var mCallback: IDialogCallback? = null

    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 5
    private var countdownTimer: MyCountDownTimer? = null

    //申贷金额
    private var showAmount = "0"
    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.MATCH_PARENT
        window!!.attributes = params
//        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    constructor(context: Context,amount: String) : super(context) {
        showAmount = amount
    }
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context,amount: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        showAmount = amount
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogApplyAmountGuideBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.applyAmountGuideCountdown.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        binding.applyAmountGuideCountdown.paint.isAntiAlias = true//抗锯齿

//        binding.guideLayout.y = guideY
        val top = ShipweTitleBarUtil.getStatusBarHeight() + AppDensityUtil.dp2px(32f)
        val left = AppDensityUtil.dp2px(16f)
        binding.applyAmountGuideViewGroup.setPadding(left,top,left,0)

        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, 5000, 1000)
        }
        countdownTimer?.start()
    }

    override fun initListener() {
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
    }

    interface IDialogCallback {
        fun onGuideDismiss()
    }

    class MyCountDownTimer(private val dialog: ApplyAmountGuideDialog, millisInFuture: Long, countDownInterval: Long): CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.showGuideTipsText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.showGuideCountDown()
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showGuideTipsText(){
        binding.applyAmountGuideCountdown.text = "${countdown}s"
    }

    private fun showGuideCountDown(){
        dismiss()
        if(mCallback != null){
            mCallback!!.onGuideDismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_GUIDE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (countdownTimer != null){
            countdownTimer = null
        }
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_GUIDE_DISMISS)
    }
}